﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;

namespace ERPStore.SvcHost
{
	public partial class MainService : ServiceBase
	{
		private ERPStore.Host.Hoster m_Hoster;

		public MainService()
		{
			InitializeComponent();
			base.ServiceName = ConfigurationManager.AppSettings["erpstore.serviceName"] ?? "ERPStoreSvcHost";
		}

		protected override void OnStart(string[] args)
		{
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

			m_Hoster = new Host.Hoster();
			m_Hoster.Start();
		}

		protected override void OnShutdown()
		{
			OnStop();
		}

		protected override void OnStop()
		{
			if (m_Hoster != null)
			{
				m_Hoster.Stop();
			}
		}

		protected override void OnContinue()
		{
			base.OnContinue();
			OnStart(null);
		}

		protected override void OnPause()
		{
			base.OnPause();
			m_Hoster.Pause();
		}

		void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			if (e.IsTerminating)
			{
				// TODO : Signal
			}
			if (e.ExceptionObject != null)
			{
				System.Diagnostics.EventLog.WriteEntry("Application", e.ExceptionObject.ToString(), System.Diagnostics.EventLogEntryType.Error);
			}
		}
	}
}
